# clip input to the bounds of EPSG:3857 _before_ projecting. this ensures that
# there are no unprojectable points, but that the data fills the all the way to
# the corners of the world.
OGR_CLIP_OPTS=-clipsrc -180 -85.051128779806604 180 85.051128779806604

all: download shapefiles

upload: shapefiles
	if aws s3 ls s3://{{ bucket }}/{{ datestamp }}/shapefiles.tar.gz; then echo 'shapefiles.tar.gz already exists for {{ datestamp }}'; exit 1; else exit 0; fi
	aws s3 cp shapefiles.tar.gz s3://{{ bucket }}/{{ datestamp }}/shapefiles.tar.gz

shapefiles: shapefiles.tar.gz

shapefiles.tar.gz: {{ tgt_shapefile_zips }} wof_snapshot.sql wikidata_snapshot.sql
	tar czf shapefiles.tar.gz $^

download: {{ src_shapefile_zips }}

{% for download in downloads %}
{{ download.tgt }}:
	wget '{{ download.url }}' -O {{ download.tgt }}
{% endfor %}

{% for shapefile in reproj_shapefiles %}
{{ shapefile.src_shp }}: {{ shapefile.src_zip }}
	unzip {% if shapefile.junk_dirs %}-j{% endif %} -o {{ shapefile.src_zip }} '{% if shapefile.junk_dirs %}*/{% endif %}{{ shapefile.src_wildcard }}'
	# touch -c means don't create the file if it doesn't already exist
	touch -c {{ shapefile.src_shp }}
	[ -f {{ shapefile.src_shp }} ]

{{ shapefile.tgt_shp }}: {{ shapefile.src_shp }}
	ogr2ogr $(OGR_CLIP_OPTS) -t_srs EPSG:3857 -lco encoding=utf8 {{ shapefile.tgt_shp }} {{ shapefile.src_shp }}
{% if shapefile.tile %}
	python tile-shapefile.py {{ shapefile.tgt_shp }} tiled-{{ shapefile.tgt_shp }}
	rm -f {{ shapefile.tgt_shp_wildcard }}
	for i in tiled-{{ shapefile.tgt_shp_wildcard }}; do mv -f $$i `echo $$i | sed s/tiled-//`; done
{% endif %}

{{ shapefile.tgt_zip }}: {{ shapefile.tgt_shp }}
	zip {{ shapefile.tgt_zip }} {{ shapefile.tgt_shp_wildcard }}

{% endfor %}

{% for shapefile in tile_shapefiles %}
{{ shapefile.src_shp }}: {{ shapefile.src_zip }}
	unzip -o {{ shapefile.src_zip }} '{{ shapefile.src_wildcard }}'
	touch {{ shapefile.src_shp }}

{{ shapefile.tgt_shp }}: {{ shapefile.src_shp }}
	python tile-shapefile.py {{ shapefile.src_shp }} {{ shapefile.tgt_shp }}

{{ shapefile.tgt_zip }}: {{ shapefile.tgt_shp }}
	zip {{ shapefile.tgt_zip }} {{ shapefile.tgt_shp_wildcard }}

{% endfor %}

{% for query in queries %}
{{ query.output_file }}:
	curl -o $@ '{{ query.url }}'

{% endfor %}

wikidata_snapshot.sql: {{ query_output_files }}
	python wikidata_merge.py --output $@ $^

wof_snapshot.sql:
	python wof_snapshot.py

clean:
	rm -rf shapefiles.tar.gz {{ tgt_shapefile_zips }} {{ tgt_shapefile_wildcards }} {{ src_shapefile_zips }} {{ src_shapefile_wildcards }} wof_snapshot.sql {{ query_output_files }} wikidata_snapshot.sql

.PHONY: all download upload shapefiles
